﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Archives : System.Web.UI.Page
{
	private string cs = System.Configuration.ConfigurationManager.ConnectionStrings["Resist"].ConnectionString;
	
	protected void Page_Load(object sender, EventArgs e)
    {
		LoadArchives();

		try { LoadArchive(DateTime.Parse(Request.QueryString["MinArch"]), DateTime.Parse(Request.QueryString["MaxArch"])); } catch { pnlArch.Visible = false; }
    }
	private void LoadArchives()
	{
		string lbl = "<table style=\"width: 100%;\"><tr>";
		DataTable dt = KC.DataAccess.Global.Global.CallSql_Query(cs, 
			"select substring(cast(cast(created as date) as varchar),1,7), count(substring(cast(cast(created as date) as varchar),1,7)) from posts " +
			"group by substring(cast(cast(created as date) as varchar),1,7) " +
			"order by substring(cast(cast(created as date) as varchar),1,7) desc ");
		for (int i = 0; i < dt.Rows.Count; i++) {
			string moyr = (string)dt.Rows[i][0];
			DateTime date = DateTime.Parse(moyr.Split('-')[1] + "/01/" + moyr.Split('-')[0]);
			lbl += "<td><a href=\"Archives.aspx?MinArch=" + date.ToString("MM/dd/yyyy") + "&MaxArch=" + date.AddMonths(1).AddDays(-1).ToString("MM/dd/yyyy") + "\">" + date.ToString("MM/yyyy") + "</a> ("+dt.Rows[i][1].ToString()+")</td>";
			if (i % 6 == 0) lbl += "</tr>\r\n<tr>"; }
		lbl += "</tr></table>";
		lblArchives.Text = lbl;
	}
	private void LoadArchive(DateTime fromDate, DateTime toDate)
	{
		DataTable dt = KC.DataAccess.Global.Global.CallSql_Query(cs, "select * from Posts where isEnabled=1 and Created between '" + fromDate.ToString("MM/dd/yyyy") + "' and '" + toDate.ToString("MM/dd/yyyy") + " 23:59:59' order by ID desc");
		pnlArch.Visible = dt.Rows.Count > 0;
		rPosts.DataSource = dt;
		lblArch.Text = fromDate.ToString("MM/dd/yyyy") + " and " + toDate.ToString("MM/dd/yyyy");
		rPosts.DataBind();
	}
	protected void rPosts_ItemDataBound(object sender, RepeaterItemEventArgs e)
	{
		if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem) return;
		DataRowView dr = (DataRowView)e.Item.DataItem;
		((HyperLink)e.Item.FindControl("hlTitle")).Text = Server.HtmlEncode(dr["Title"].ToString());
		((HyperLink)e.Item.FindControl("hlTitle")).NavigateUrl = "http://www.ResistOccupation.com/Post.aspx?" + dr["FriendlyName"].ToString();
		((Label)e.Item.FindControl("lblDate")).Text = ((DateTime)dr["Created"]).ToString("MM/dd/yyyy hh:mm ttt");
		string body = common.StripTagsCharArray(dr["Body"].ToString());
		if (body.Length > 1024) body = body.Substring(0, 1018) + "<a href=\"http://www.ResistOccupation.com/Post.aspx?" + dr["FriendlyName"].ToString() + "\" alt=\"Continue Reading \"> [...] </a>";
		((Label)e.Item.FindControl("lblBody")).Text = body;

		// Load "Posted In" category list
		DataTable dtCats = KC.DataAccess.Global.Global.CallSql_Query(cs, "select * from Categories where ID in (select CategoryID from PostCategoryMap where PostID=" + dr["ID"].ToString() + ") order by Name");
		string cats = "";
		foreach (DataRow cat in dtCats.Rows)
			cats += "<a href=\"Categories.aspx?" + cat["FriendlyName"].ToString() + "\" Title=\"" + Server.HtmlEncode(cat["Description"].ToString()) + "\">" + cat["Name"].ToString() + "</a>, ";
		((Label)e.Item.FindControl("lblCats")).Text = cats.TrimEnd(new char[] { ',', ' ' });
	}
}